package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.mapper.AddressBookMapper;
import com.sky.entity.AddressBook;
import com.sky.service.AddressBookService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地址簿 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public void setDefault(AddressBook addressBook) {
        //先将该用户的所以地址设置为非默认地址
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        this.update(addressBookLambdaUpdateWrapper);
        //再将该用户该地址设置为默认地址
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        this.updateById(addressBook);
    }
}
